package draw_ui;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-03-13
 * Time: 21:39
 */
public class GameJFrame extends JFrame implements KeyListener , ActionListener {

    int[][] arr = new int[4][4];

    int count = 0;

    JMenuItem replayItem = new JMenuItem("重新游戏");
    JMenuItem reloginItem = new JMenuItem("重新登录");
    JMenuItem closeItem = new JMenuItem("关闭游戏");

    JMenuItem accountItem = new JMenuItem("公众号");

    public GameJFrame(){

        initJFrame();

        initJMenubar();

        initData();

        initTmage();

        //设置界面显示
        this.setVisible(true);
    }

    int x = 0;
    int y = 0;

    private void initData() {
        int[] arr1 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
        Random random = new Random();
        for (int i = 0; i < arr1.length; i++) {
            int tmpi = random.nextInt(arr1.length);
            int tmp = arr1[i];
            arr1[i] = arr1[tmpi];
            arr1[tmpi] = tmp;
        }

        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] == 0){
                x = i / 4;
                y = i % 4;
            }
            arr[i / 4][i % 4] = arr1[i];
        }
    }

    private void initTmage(){

        this.getContentPane().removeAll();

        if (victory()) {
            //显示胜利图标
        }

        JLabel septCount = new JLabel("步数: "+count);
        septCount.setBounds(50,30,100,20);
        this.getContentPane().add(septCount);

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                int tmp = arr[i][j];
                ImageIcon icon = new ImageIcon("+tmp+");//空格填路径
                JLabel jLabel1 = new JLabel(icon);
                jLabel1.setBounds(105 * j + 83,105 * i + 134,105,105);
                //this.add(jLabel);

                jLabel1.setBorder(new BevelBorder(0));

                this.getContentPane().add(jLabel1);
            }
        }

        this.getContentPane().repaint();

        //设置背景图片
        JLabel jLabel = new JLabel(new ImageIcon());
        jLabel.setBounds(40,40,508,560);
        this.getContentPane().add(jLabel);

        for (int i = 0; i < 4; i++) {
            ImageIcon icon = new ImageIcon();//空格填路径
            JLabel jLabel1 = new JLabel(icon);
            jLabel1.setBounds(105 * i,0,105,105);
            //this.add(jLabel);
            this.getContentPane().add(jLabel1);
        }

        for (int i = 0; i < 4; i++) {
            ImageIcon icon = new ImageIcon();//空格填路径
            JLabel jLabel1 = new JLabel(icon);
            jLabel1.setBounds(105 * i,105,105,105);
            //this.add(jLabel);
            this.getContentPane().add(jLabel1);
        }

        for (int i = 0; i < 4; i++) {
            ImageIcon icon = new ImageIcon();//空格填路径
            JLabel jLabel1 = new JLabel(icon);
            jLabel1.setBounds(105 * i,210,105,105);
            //this.add(jLabel);
            this.getContentPane().add(jLabel1);
        }

        for (int i = 0; i < 4; i++) {
            ImageIcon icon = new ImageIcon();//空格填路径
            JLabel jLabel1 = new JLabel(icon);
            jLabel1.setBounds(105 * i,315,105,105);
            //this.add(jLabel);
            this.getContentPane().add(jLabel1);
        }
    }



    private void initJMenubar() {
        JMenuBar jMenuBar = new JMenuBar();
        JMenu functionJmenu = new JMenu("功能");
        JMenu aboutJmenu = new JMenu("关于我们");


        functionJmenu.add(replayItem);
        functionJmenu.add(reloginItem);
        functionJmenu.add(closeItem);

        aboutJmenu.add(accountItem);

        replayItem.addActionListener(this);
        reloginItem.addActionListener(this);
        closeItem.addActionListener(this);
        aboutJmenu.addActionListener(this);

        jMenuBar.add(functionJmenu);
        jMenuBar.add(aboutJmenu);

        this.setJMenuBar(jMenuBar);
    }

    private void initJFrame() {
        //设置界面宽高
        this.setSize(603,608);

        //设置界面标题
        this.setTitle("拼图游戏单机版 v1.0");

        //设置界面置顶
        this.setAlwaysOnTop(true);

        //设置界面居中
        this.setLocationRelativeTo(null);

        //关闭模式
        this.setDefaultCloseOperation(3);

        //取消默认的居中方式

        this.setLayout(null);

        this.addKeyListener(this);


    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        if (code == 65){
            this.getContentPane().removeAll();
            JLabel all = new JLabel(new ImageIcon());
            all.setBounds(83,134,420,420);
            this.getContentPane().add(all);
            JLabel jLabel = new JLabel(new ImageIcon());
            jLabel.setBounds(40,40,508,560);
            this.getContentPane().add(jLabel);
            this.getContentPane().repaint();

        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (victory()) {
            return;
        }
        int code = e.getKeyCode();
        if (code == 37){
            if(y == 3){
                return;
            }
            count++;
            System.out.println("向左移动");
            arr[x][y] = arr[x][y + 1];
            arr[x][y + 1] = 0;
            initTmage();
        }else if (code == 38){
            if (x == 3){
                return;
            }
            count++;
            System.out.println("向下移动");
            arr[x][y] = arr[x + 1][y];
            arr[x + 1][y] = 0;
            initTmage();
        }else if (code == 39){
            if (y == 0){
                return;
            }
            count++;
            System.out.println("向右移动");
            arr[x][y] = arr[x][y - 1];
            arr[x][y - 1] = 0;
            initTmage();
        }else if (code == 40){
            if (x == 0){
                return;
            }
            count++;
            arr[x][y] = arr[x - 1][y];
            arr[x - 1][y] = 0;
            initTmage();
            System.out.println("向下移动");
        }else if (code == 65){
            initTmage();
        }else if (code == 87){
            arr = new int[][]{
                    {1,2,3,4},
                    {5,6,7,8},
                    {9,10,11,12},
                    {13,14,15,0}
            };
            initTmage();

        }

    }

    int[][] win = { {1,2,3,4},
                    {5,6,7,8},
                    {9,10,11,12},
                    {13,14,15,0}
    };

    public boolean victory(){
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (arr[i][j] != win[i][j]){
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        if (obj == replayItem){
            System.out.println("重新游戏");
            count = 0;
            initData();
            initTmage();

        }else if (obj == reloginItem){
            System.out.println("重新登录");
            this.setVisible(false);
            new LoginJFrame();
        }else if (obj == closeItem){
            System.out.println("关闭游戏");
            System.exit(0);
        } else if (obj == accountItem) {
            System.out.println("关于公众号");
            JDialog jDialog = new JDialog();
            JLabel jLabel = new JLabel(new ImageIcon());//输入图片路径
            jLabel.setBounds(0,0,258,258);
            jDialog.getContentPane().add(jLabel);
            jDialog.setSize(344,344);
            jDialog.setAlwaysOnTop(true);
            jDialog.setLocationRelativeTo(null);
            jDialog.setModal(true);
            jDialog.setVisible(true);

        }
    }
}
